<template>
  <div class="app-container">
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)"
          >{{ $t('add') }}
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="menuList"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildrens'}"
      >
        <el-table-column prop="name" :label="$t('name')" :show-overflow-tooltip="true" width="180">
          <template slot-scope="scope">
            <i>{{ getTitle(scope.row.title) }}</i>
          </template>
        </el-table-column>
        <el-table-column prop="icon" :label="$t('icon')" width="100">
          <template slot-scope="scope">
            <i :class="scope.row.icon"/>
          </template>
        </el-table-column>
        <el-table-column prop="sort" :label="$t('rank')" width="60"/>
        <el-table-column prop="path" :label="$t('path')" :show-overflow-tooltip="true"/>
        <el-table-column prop="type" :label="$t('type')" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === '0'" type="success">{{ $t('menu') }}</el-tag>
            <el-tag v-if="scope.row.type === '1'" type="info">{{ $t('button') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="keepAlive" :label="$t('buffer')" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.keepAlive === '0'" type="info">{{ $t('close') }}</el-tag>
            <el-tag v-if="scope.row.keepAlive === '1'" type="success">{{ $t('open') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="permission" :label="$t('permission')" :show-overflow-tooltip="true"/>
        <el-table-column :label="$t('function')" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false,scope.row.id)"
            >{{ $t('add') }}
            </el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true,scope.row.id)"
            >{{ $t('edit') }}
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >{{ $t('delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="treeCompanyMenu"/>
    </div>
  </div>
</template>

<script>
import { deleteCompanyMenu, treeCompanyMenu } from '@/api/iot/iotCompanyMenuApi'
import TableForm from './menu-form'
import { mapGetters } from 'vuex'
import { getCompanyId } from '@/util/storage'
import { successNotice } from '@/util/common'

export default {
  name: 'CompanyMenu',
  components: { TableForm },
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      companyId: getCompanyId()
    }
  },
  created() {
    this.treeCompanyMenu()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    getTitle(title) {
      if (!title) {
        return ''
      }
      let str = ''
      title.split(' ').forEach(s => {
        str = str + this.$t(s)
      })
      return str
    },
    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },
    treeCompanyMenu() {
      this.loading = true
      treeCompanyMenu({
        'companyId': this.companyId,
        'lazy': false
      }).then(res => {
        const { data } = res
        this.menuList = data
        this.loading = false
      })
    },
    handleDelete(row) {
      const that = this
      this.$confirm(this.$t('deletePrompt'), this.$t('prompt'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(function() {
        return deleteCompanyMenu({ 'menuId': row.id })
      }).then(() => {
        this.treeCompanyMenu()
        successNotice(that)
      })
    }
  }
}
</script>
